package cn.itcast.bos.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.maven.surefire.shade.org.codehaus.plexus.util.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.constant.Constants;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.utils.BaseAction;
import cn.itcast.crm.domain.Customer;
@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class OrderAction extends BaseAction<Order> {
	
	private String sendAreaInfo;
	private String recAreaInfo; 
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}
	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}
	@Action(value="order_save",results={@Result(name="success",type="redirect",location="index.html")})
	public String saveOrder() {
		if(StringUtils.isNotBlank(sendAreaInfo)){
			String[] sendAreaStr = sendAreaInfo.split("/");
			Area sendArea = new Area();
			sendArea.setProvince(sendAreaStr[0]);
			sendArea.setCity(sendAreaStr[1]);
			sendArea.setDistrict(sendAreaStr[2]);
			model.setSendArea(sendArea);
		}
		if(StringUtils.isNotBlank(recAreaInfo)){
			String[] recAreaStr = recAreaInfo.split("/");
			Area recArea = new Area();
			recArea.setProvince(recAreaStr[0]);
			recArea.setCity(recAreaStr[1]);
			recArea.setDistrict(recAreaStr[2]);
			model.setRecArea(recArea);
		}
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());
		
		WebClient.create(Constants.BOS_MANAGEMENT_URL+"/services/orderService/saveOrder").type(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;
	}
}
